{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" width=\"500 px\" align=\"center\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# _*Qiskit Aqua: Chemistry basic how to*_ \n",
    "\n",
    "\n",
    "This notebook is based on an official notebook by Qiskit team, available at https://github.com/qiskit/qiskit-tutorial under the [Apache License 2.0](https://github.com/Qiskit/qiskit-tutorial/blob/master/LICENSE) license. \n",
    "The original notebook was developed by Richard Chen<sup>[1]</sup>, Antonio Mezzacapo<sup>[1]</sup>, Marco Pistoia<sup>[1]</sup>, Stephen Wood<sup>[1]</sup> (<sup>[1]</sup>IBMQ)\n",
    "\n",
    "Your **TASK** is to execute every step of this notebook while learning to use qiskit-aqua and also how to simulate atomic phenomena with quantum devices.\n",
    "\n",
    "(requires instalation of **qiskit-aqua-chemistry** with `pip install qiskit-aqua-chemistry`)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introduction\n",
    "\n",
    "This notebook demonstrates how to use Qiskit Aqua Chemistry to compute the ground state energy of a Hydrogen (H2) molecule using VQE and UCCSD.\n",
    "\n",
    "This notebook has been written to use the HDF5 chemistry driver. This driver uses molecular data that has been saved from a prior computation so that this notebook can be run with no additional driver installation requirements. See the HDF5 chemistry driver readme for more detail.\n",
    "\n",
    "First we import QiskitChemistry, which is the object that will carry out the computation for us"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.chemistry import QiskitChemistry"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we create a Python dictionary to specify the problem we want to solve. There are defaults for many additional values that are not show here for simpicity. Indeed we take advantage of using sensisble defaults that the qischem stack provides to help us here.  Please notice that the Qiskit Aqua Chemistry GUI allows for automatic extraction of the Python dictionary reflecting the current configuration.  Once the Python dictionary has been extracted, it can be pasted into a Python program or a Jupyter Notebook and, if necessary, edited.\n",
    "\n",
    "The first entry names a chemistry driver. This example uses HDF5 and the next line configures the driver for an hdf5 file that contains data from a prior computation for an H2 molecule with basis set sto-3g. The operator line would default but I have added it here to show it and to say that this is where the problem is converted into a quantum qubit form. We then have a VQE algorithm, using the COBYLA optimizer with a UCCSD variatonal form and initial state of HartreeFock. VQE is Variational Quantum Eigensolver and as its name suggests uses a variational method to find the mimimum eigenvalue of the problem, which in this case is the ground state energy of the molecule."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [Optional] Setup token to run the experiment on a real device\n",
    "If you would like to run the experiement on a real device, you need to setup your account first.\n",
    "\n",
    "Note: If you do not store your token yet, use `IBMQ.save_accounts()` to store it first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import IBMQ\n",
    "IBMQ.load_account()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Input dictionary to configure Qiskit AQUA Chemistry for the chemistry problem.\n",
    "qiskit_chemistry_dict = {\n",
    "    'driver': {'name': 'HDF5'},\n",
    "    'HDF5': {'hdf5_input': '0.7_sto-3g.hdf5'},\n",
    "    'operator': {'name': 'hamiltonian'},\n",
    "    'algorithm': {'name': 'VQE'},\n",
    "    'optimizer': {'name': 'COBYLA'},\n",
    "    'variational_form': {'name': 'UCCSD'},\n",
    "    'initial_state': {'name': 'HartreeFock'},\n",
    "    'backend': {'name': 'statevector_simulator'}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now create a QiskitChemistry object and call run on it passing in the problem dictionary to get a result. This may take a short time and it will use a local quantum simulator to carry out the quantum computation that the VQE algorithm uses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "solver = QiskitChemistry()\n",
    "result = solver.run(qiskit_chemistry_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The run method returns a result dictionary. Some notable fields include 'energy' which is the computed ground state energy. We can print it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ground state energy: -1.1361894321408244\n"
     ]
    }
   ],
   "source": [
    "print('Ground state energy: {}'.format(result['energy']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is also a 'printable' field containing a complete ready to print readable result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== GROUND STATE ENERGY ===\n",
      " \n",
      "* Electronic ground state energy (Hartree): -1.892156876312\n",
      "  - computed part:      -1.892156876312\n",
      "  - frozen energy part: 0.0\n",
      "  - particle hole part: 0.0\n",
      "~ Nuclear repulsion energy (Hartree): 0.755967444171\n",
      "> Total ground state energy (Hartree): -1.136189432141\n",
      "  Measured:: Num particles: 2.000, S: 0.000, M: 0.00000\n",
      " \n",
      "=== DIPOLE MOMENT ===\n",
      " \n",
      "* Electronic dipole moment (a.u.): [0.0  0.0  0.00029555]\n",
      "  - computed part:      [0.0  0.0  0.00029555]\n",
      "  - frozen energy part: [0.0  0.0  0.0]\n",
      "  - particle hole part: [0.0  0.0  0.0]\n",
      "~ Nuclear dipole moment (a.u.): [0.0  0.0  0.0]\n",
      "> Dipole moment (a.u.): [0.0  0.0  -0.00029555]  Total: 0.00029555\n",
      "               (debye): [0.0  0.0  -0.00075122]  Total: 0.00075122\n"
     ]
    }
   ],
   "source": [
    "for line in result['printable']:\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This was a very simple example showing how to get started. There are more elaborate notebooks here as well documentation describing the various components and their configurations to help you to experiment with quantum computing and its application to solving chemistry problems."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
